package com.fengwk.support.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.fengwk.support.exception.BaseException;
import com.fengwk.support.function.Return;
import com.fengwk.support.function.Void;

public class MybatisUtils {

	public static final int MAX_FOREACH_GROUP_SIZE = 500;
	
	private MybatisUtils() {
		throw new BaseException("can not instance " + getClass());
	}
	
	/**
	 * 分组查询,解决 mybtis foreach 问题
	 * 
	 * @param params
	 * @param queryFun
	 * @return
	 */
	public static <S, T> List<T> groupQueryWithReturn(Collection<S> params, Return.Params1<List<T>, List<S>> queryFun) {
		List<T> list = new ArrayList<T>();
		groupQueryWithProcess(params, group -> {
			List<T> listGroup = queryFun.apply(group);
			if (CollectionUtils.isNotBlank(listGroup)) {
				list.addAll(listGroup);
			}
		});
		return list;
	}
	
	/**
	 * 分组查询并执行
	 * 
	 * @param params
	 * @param queryFun
	 */
	public static <E> void groupQueryWithProcess(Collection<E> params, Void.Params1<List<E>> queryFun) {
		CollectionUtils.splitCollectionByGroupSizeWithProcess(params, MAX_FOREACH_GROUP_SIZE, queryFun);
	}
	
}
